package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Singer;
import com.entity.SingerType;
import com.entity.Data.PagerData;
import com.entity.Query.SingerQuery;
import com.service.SingerService;
import com.service.SingerTypeService;
import com.util.MyUtil;

/**
 * 歌手
 * @author Shinelon
 *
 */
@RequestMapping("/singer")
@Controller
public class SingerController {
	
	@Autowired
	private SingerService singerService;
	@Autowired
	private SingerTypeService singerTypeService;
	
	@RequestMapping("/singer_show")
	public String singer_show(Model model,SingerQuery singerQuery,Integer curPage) {
		//如果没有页数，则页数默认为1
		if(curPage==null){
			curPage = 1;
		}
		
		//带条件分页查询
		PagerData pd = singerService.queryByPage(singerQuery, curPage, 5);
		model.addAttribute("pd", pd);
		
		List<Map<String, Object>> singerTypeList = singerTypeService.querySingerType();
		model.addAttribute("singerTypeList", singerTypeList);
		return "admin/singer";
	}
	
	@RequestMapping("/singer_add_show")
	public String singer_add_show(Model model) {
		List<Map<String, Object>> singerTypeList = singerTypeService.querySingerType();
		model.addAttribute("singerTypeList", singerTypeList);
		return "admin/singer-add";
	}
	
	@RequestMapping("/singer_add_submit")
	public String singer_add_submit(@RequestParam(required=false) MultipartFile[] wj,HttpServletRequest request) {
		Singer singer = new Singer();
		//获取保存文件的服务器目录
				String saveFilePath = request.getServletContext().getRealPath("myfile/singer/");
				
				//遍历每个上传文件进行保存
				for(MultipartFile mf : wj){
					//文件名
					String oldFileName = mf.getOriginalFilename();
					//扩展名
					String ext = FilenameUtils.getExtension(oldFileName);
					System.out.println(oldFileName+" "+ext);
					//新的文件名
					String newFileName = UUID.randomUUID()+"_"+oldFileName;
					//构建要保存的文件对象
					File f=new File(saveFilePath,newFileName);
					
					if(!f.exists()){
						f.mkdirs();
					}
					
					//将上传文件存到指定的文件对象上
					try {
						mf.transferTo(f);
						if("png".equals(ext)){
							singer.setSingerPhoto("myfile/singer/"+newFileName);
						}else if("jpg".equals(ext)){
							singer.setSingerPhoto("myfile/singer/"+newFileName);
						}
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				String singerName = request.getParameter("singerName");
				String seName = request.getParameter("seName");
				String state = request.getParameter("state");
				String address = request.getParameter("address");
				String work = request.getParameter("work");
				String birthday = request.getParameter("birthday");
				String singerTypeId = request.getParameter("singerTypeId");
				singer.setSingerName(singerName);
				singer.setSeName(seName);
				singer.setState(state);
				singer.setAddress(address);
				singer.setWork(work);
				singer.setBirthday(MyUtil.getDate(birthday));
				singer.setSingerTypeId(Integer.parseInt(singerTypeId));
		singerService.addSinger(singer);
		return "admin/singer-add";
	}
	
	@RequestMapping("/singer_update_show")
	public String singer_update_show(Integer singerId,Model model) {
		Map<String, Object> singerMap = singerService.querySingerId(singerId);
		model.addAttribute("singerMap", singerMap);
		List<Map<String, Object>> singerTypeList = singerTypeService.querySingerType();
		model.addAttribute("singerTypeList", singerTypeList);
		return "admin/singer-edit";
	}
	
	@RequestMapping("/singer_update_submit")
	public String singer_update_submit(Singer singer) {
		singerService.editSinger(singer);
		return "admin/singer-edit";
	}
	
	@RequestMapping("/singer_delete")
	public String singer_delete(Integer singerId) {
		singerService.delSinger(singerId);
		return "admin/singer";
	}
}
